package com.songlanyun.modules.usercart.controller.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.usercart.model.dto.UserCartDTO;
import com.songlanyun.modules.usercart.model.vo.UserCartGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.usercart.entity.UserCart;
import com.songlanyun.modules.usercart.service.UserCartService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;



/**
 * 会员的购物车表
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-12 09:27:04
 */
@RestController
@RequestMapping("api/cart")
public class UserCartApiController extends AbstractApiController {
    @Autowired
    private UserCartService userCartService;

    /**
     * 获取当前用户的购物车
     */
    @RequestMapping("/list/{shopId}")
    //@RequiresPermissions("usercart:saleusershoppingcart:list")
    public R list(@PathVariable("shopId")Long shopId){
        Long userId = getUserId(false);
        List<UserCartGroupVo> list = new ArrayList<>();
        if(userId==0)
        {
            return R.ok().put("list", list);
        }
        list = userCartService.myCarts(shopId,getUserId(false));
        return R.ok().put("list", list);
    }


    /**
     * 添加商品到购物车
     * shopId 就是当前的来原
     */
    @RepeatSubmit(value = "5")
    @PostMapping("/add")
    //@RequiresPermissions("usercart:saleusershoppingcart:save")
    public R add(@RequestBody UserCartDTO userCartDTO){

		UserCart cart = userCartService.add(getUserId(true),userCartDTO);

        return R.ok().put("data",cart);
    }

    /**
     * 设置商品购物车的数量
     */
    @RepeatSubmit(value = "5")
    @RequestMapping("/num/{shopId}")
    //@RequiresPermissions("usercart:saleusershoppingcart:save")
    public R setNum(@PathVariable("shopId") Long shopId,@RequestBody UserCartDTO userCartDTO){

        UserCart cart = userCartService.setNum(shopId,getUserId(true),userCartDTO);

        return R.ok().put("data",cart);
    }


    /**
     * 删除商品购物车
     */
    @RepeatSubmit(value = "3")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		userCartService.remove(new LambdaQueryWrapper<UserCart>()
                .eq(UserCart::getUserId,getUserId(true))
                .in(UserCart::getId,Arrays.asList(ids)));
        return R.ok();
    }

}
